﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    public static partial class IdentifierAndValueExtensions
    {
        public static List<IdentifierAndValue> MakeArgumentCopy(this List<IdentifierAndValue> args)
        {
            List<IdentifierAndValue> results = new List<IdentifierAndValue>();

            for (int n = 0; n < args.Count; n++)
            {
                results.Add(new IdentifierAndValue() { Identifier = args[n].Identifier, Value = args[n].Value });
            }

            return results;
        }

        public static T GetArgument<T>(this List<IdentifierAndValue> args, int index)
        {
            if (args != null && args.Count > index)
            {
                return (T)args[index].Value;
            }

            return default(T);
        }

        public static T GetArgument<T>(this List<IdentifierAndValue> args, int index, object defaultValue)
        {
            T item = GetArgument<T>(args, index);

            if (item == null)
                return (T)defaultValue;

            return item;
        }

        public static T GetArgument<T>(this List<IdentifierAndValue> args, string identifier)
        {
            if (args != null)
            {
                for (int n = 0; n < args.Count; n++)
                {
                    if (args[n].Identifier.Equals(identifier, StringComparison.OrdinalIgnoreCase))
                        return (T)args[n].Value;
                }
            }

            return default(T);
        }

        public static IdentifierAndValue GetArgumentReference(this List<IdentifierAndValue> args, string identifier)
        {
            if (args != null)
            {
                for (int n = 0; n < args.Count; n++)
                {
                    if (args[n].Identifier.Equals(identifier, StringComparison.OrdinalIgnoreCase))
                        return args[n];
                }
            }

            return null;
        }

        public static T GetArgument<T>(this List<IdentifierAndValue> args, string identifier, object defaultValue)
        {
            IdentifierAndValue item = GetArgumentReference(args, identifier);

            if (item.Value == null)
            {
                object o = Convert.ChangeType(defaultValue, typeof(T), null);
                return (T)o;
            }

            return (T)item.Value;
        }

        public static T GetArgument<T>(this List<IdentifierAndValue> args)
        {
            if (args != null && args.Count > 0)
            {
                return (T)args[0].Value;
            }

            return default(T);
        }

        public static bool GetArguments<T1, T2, T3, T4>(this List<IdentifierAndValue> args, ref T1 arg1, ref T2 arg2, ref T3 arg3, ref T4 arg4)
        {
            if (args != null && args.Count > 3)
            {
                arg1 = (T1)args[0].Value;
                arg2 = (T2)args[1].Value;
                arg3 = (T3)args[2].Value;
                arg4 = (T4)args[3].Value;
                return true;
            }

            return false;
        }

        public static bool GetArguments<T1, T2, T3>(this List<IdentifierAndValue> args, ref T1 arg1, ref T2 arg2, ref T3 arg3)
        {
            if (args != null && args.Count > 2)
            {
                arg1 = (T1)args[0].Value;
                arg2 = (T2)args[1].Value;
                arg3 = (T3)args[2].Value;
                return true;
            }

            return false;
        }

        public static bool GetArguments<T1, T2>(this List<IdentifierAndValue> args, ref T1 arg1, ref T2 arg2)
        {
            if (args != null && args.Count > 1)
            {
                arg1 = (T1)args[0].Value;
                arg2 = (T2)args[1].Value;
                return true;
            }

            return false;
        }

        public static T GetArgument<T>(this List<IdentifierAndValue> args, object defaultValue)
        {
            T item = GetArgument<T>(args);

            if (default(T).Equals(item))
                return (T)defaultValue;

            return item;
        }

        public static int TryInt(this object o)
        {
            if (o == null)
                return 0;

            int n;

            if (int.TryParse(o.ToString(), out n))
                return n;

            return 0;
        }

        public static string StringOrEmpty(this string[] strings, int index)
        {
            if (strings.Length > index)
                return strings[index];

            return string.Empty;
        }

        public static int Int(this object o)
        {
            if (o == null)
                return 0;

            return Convert.ToInt32(o);
        }

        public static long Long(this object o)
        {
            if (o == null)
                return 0;

            return Convert.ToInt64(o);
        }
    }
}